

//this js file takes the Map array and preforms polite way finding functions 

function get_my_room(x,y,rooms)
{
	//uses intenal coords
	var rx = Math.floor(x*(rooms.length)/100);
	var ry = Math.floor(y*(rooms[0].length)/100);
	return [rx,ry];
}


function get_room_center(rooms,x,y)
{
	var width = 100/rooms.length;
	var height = 100/rooms[0].length;

	x = width*(x+0.5);
	y = height*(y+0.5);

	return [x,y];
}

function get_adjacent_rooms(X,Y,rooms)
{
	var output = Array();
	here = rooms[X][Y];
	if(here.north)
	{
		output.push(rooms[X][Y-1]);
	}
	if(here.east)
	{
		output.push(rooms[X+1][Y]);
	}
	if(here.south)
	{
		output.push(rooms[X][Y+1]);
	}
	if(here.west)
	{
		output.push(rooms[X-1][Y]);
	}
	////alert("get adj:"+output)
	return output;


}






function advise_crewman(c,thisroom,rooms)
{
	var info = 0;
	
	if(c.path.length>0)
	{
		//alert(c.path.length);
		var lastplace = c.path[c.path.length-1];
		for(var i=0;i<c.path.length;i++)
		{
			if(thisroom.paths["_"+c.path[i].x+","+c.path[i].y]==undefined)
			{
				info+=1;
				thisroom.paths["_"+c.path[i].x+","+c.path[i].y]=lastplace;
			}
		}
	}
	//console.log("info gained "+info);
	if(thisroom.paths["_"+c.destiation[0]+","+c.destiation[1]]!=undefined)
	{
		c.path.push(thisroom);
		return thisroom.paths["_"+c.destiation[0]+","+c.destiation[1]]

	}
	else
	{
		var peers = get_adjacent_rooms(thisroom.x,thisroom.y,rooms);
		var picked = Math.floor(Math.random()*peers.length);
		c.path.push(thisroom);
		return peers[picked];

	}
}




	//time to A*






function get_dist(x0,y0,x1,y1)
{
	return Math.sqrt((x0-x1)*(x0-x1)+(y0-y1)*(y0-y1));

}



